package com.xzr.hospitalregister.util;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Locale;

/**
 * Created by Administrator on 2018/1/31 0031.
 */

public class GetTokenFromClientAPK {

    //2、平台鉴权Token
    public static String getTokenPublic(Context context) {
        String ipTvToken = "";
        Uri uri = Uri.parse("content://stbconfig/authentication/user_token");
        try {
            Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
            if (mCursor != null) {
                while (mCursor.moveToNext()) {
                    ipTvToken = mCursor.getString(mCursor.getColumnIndex("value"));
//                    PublicInfoAll.setToken(ipTvToken);
//                    MyMessageUtil.getInstance().showLogE("获取Token为:" + ipTvToken);
                }
                mCursor.close();
            } else {
//                MyMessageUtil.getInstance().showTask(context, "Token信息获取为空");
            }
        } catch (IllegalArgumentException e) {
//            MyMessageUtil.getInstance().showTask(context, "Token终端APK版本出错");
        } catch (Exception e) {
//            MyMessageUtil.getInstance().showTask(context, "获取Token失败");
        }
        return ipTvToken;
    }


    /**
     * get device mac address
     *
     * @return this device mac address
     */
    public static String getMac() {
        String macSerial = null;
        String str = "";
        try {
//            MyMessageUtil.getInstance().showContent("进来uuid获取");
            Process pp = Runtime.getRuntime().exec("cat/sys/class/net/wlan0/address");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
//            MyMessageUtil.getInstance().showContent("LineNumberReader input[" + input.toString() + "]");
            while (null != str) {
                str = input.readLine();
//                MyMessageUtil.getInstance().showContent("进来For循环【" + str + "】");
                if (str != null) {
//                    MyMessageUtil.getInstance().showContent("进来if[" + str + "]");
                    macSerial = str.trim();
                    break;
                }
            }
        } catch (IOException ex) {
            // io exception and you can ignore or set default mac
//            MyMessageUtil.getInstance().showContent("进入抛异常[" + str + "][" + ex.toString() + "]");
            ex.printStackTrace();
        }
        return macSerial;
    }

    public static String getMacAddress() {
        try {
            NetworkInterface e = NetworkInterface.getByName("eth0");
//            MyMessageUtil.getInstance().showContent("var1 【"+e.toString()+"】");
            return e == null ? null : ConvertMacAddressBytesToString(e.getHardwareAddress());
        } catch (Exception var1) {
//            MyMessageUtil.getInstance().showContent("var1 【"+var1.toString()+"】");
            return null;
        }
    }

    private static String ConvertMacAddressBytesToString(byte[] hardwareAddress) {
        return new String(hardwareAddress);
    }


    public static String getMacAddress1() {
        String address = null;
        try {
            Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
            while(true) {
                NetworkInterface netWork;
                byte[] by;
                do {
                    do {
                        do {
                            if(!interfaces.hasMoreElements()) {
                                return address;
                            }

                            netWork = (NetworkInterface)interfaces.nextElement();
                        } while(netWork == null);

                        by = netWork.getHardwareAddress();
                    } while(by == null);
                } while(by.length == 0);

                StringBuilder builder = new StringBuilder();
                byte[] var8 = by;
                int var7 = by.length;

                for(int var6 = 0; var6 < var7; ++var6) {
                    byte mac = var8[var6];
                    builder.append(String.format("%02X:", new Object[]{Byte.valueOf(mac)}));
                }

                if(builder.length() > 0) {
                    builder.deleteCharAt(builder.length() - 1);
                }

                String var10 = builder.toString();
                if(netWork.getName().equals("eth0")) {
                    address = var10;
                }
            }
        } catch (Exception var9) {
            return address;
        }
    }


    public static String getMacAddress2() {
        String str = "";
        String macSerial = "";

        try {
            Process e = Runtime.getRuntime().exec("cat/sys/class/net/wlan0/address");
            InputStreamReader ir = new InputStreamReader(e.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);

            while(str != null) {
                str = input.readLine();
                if(str != null) {
                    macSerial = str.trim();
                    break;
                }
            }
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        if(macSerial == null || "".equals(macSerial)) {
            try {
                return loadFileAsString("/sys/class/net/eth0/address").toUpperCase(Locale.getDefault()).substring(0, 17);
            } catch (Exception var5) {
                var5.printStackTrace();
            }
        }

        return macSerial != null && !"".equals(macSerial)?macSerial:"unknown";
    }

    public static String loadFileAsString(String fileName) throws Exception {
        FileReader reader = new FileReader(fileName);
        String text = loadReaderAsString(reader);
        reader.close();
        return text;
    }

    public static String loadReaderAsString(Reader reader) throws Exception {
        StringBuilder builder = new StringBuilder();
        char[] buffer = new char[4096];

        for(int readLength = reader.read(buffer); readLength >= 0; readLength = reader.read(buffer)) {
            builder.append(buffer, 0, readLength);
        }

        return builder.toString();
    }
}
